package com.feifan.Filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServletFilter implements Filter {

    @Override
    public void destroy() {
        System.out.println("=========过滤器销毁了=================");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse res = (HttpServletResponse)response;
        System.out.println("过滤器执行操作" + req.getQueryString());
        req.setCharacterEncoding("utf-8");

        // 解决浏览器中文乱码
        res.setHeader("content-type", "text/html;charset=utf-8");
        // 解决get请求乱码
        MyRequestWarp reqs = new MyRequestWarp(req);
        chain.doFilter(reqs, res);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("过滤器初始化");
    }

}
